package day05;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.plaf.synth.SynthSpinnerUI;

import day02.Title;


/**
 * 模拟数据库
 * @author bonnie
 * 
 */
public class Shujuku {
	private Map<Integer, List<Title>> title=new HashMap<Integer, List<Title>>();
	Map<String,User> usermap=new HashMap<String, User>();
	private String Rulee=null;
	
	public Shujuku() {
		this.title=title;
		titleStatr();
		uerstart();
		rulestart();
	}
	// rules
	



	// 试卷解析
	private void titleStatr() {
//		String pp=Shujuku.class.getResource("../../../../srk.txt").getFile();
		BufferedReader rf=null;
		try {
			rf = new BufferedReader(
					new InputStreamReader(
							new FileInputStream("srk.txt")));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
   	 	String str=null;

   	 	try {
			while((str=rf.readLine())!=null) {//读取第一行并进行解析存入Title
					String n=str;
					String[] nn=n.split(",");
					Title tt=new Title();
					int level =Integer.parseInt(nn[2].substring(nn[2].indexOf("=")+1,(nn[2].length())));
					tt.setLevel(level);
					tt.setScore(nn[1]);   	 			
					String anwsercut=nn[0].substring(nn[0].indexOf("=")+1,(nn[0].length()));   			
			
					if(anwsercut.length()==1) {
						tt.setChoose(1);
					}else if(anwsercut.length()<5){
						tt.setChoose(2);
					}else {
						tt.setChoose(3);
					}
					String [] anwsercutmore=anwsercut.split("/");
					ArrayList<Integer> anwserlist=new ArrayList<Integer>();
					for(String s:anwsercutmore) {
						int ss=Integer.valueOf(s);
						anwserlist.add(ss);
					}
					tt.setAnwser(anwserlist);

					String m=rf.readLine();//读取第二行存入Title
					tt.setQuestion(m);
					List list= new ArrayList();  	 			
					for(int i=0;i<4;i++) {//读取后面四行选项
						list.add(rf.readLine());
				    }
					tt.setXuanxiang(list);//将选项添加到Title中
					//将不同难度的题目加入到map中
					
					//title -->map类
					//tt--> 试题类
					//al-->集合类
					if(title.containsKey(tt.level)==false){
						ArrayList al=new ArrayList();
						al.add(tt);
						title.put(tt.level, al);
					}else {
						title.get(tt.level).add(tt);
					}		
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
   	 	
//   	 	System.out.println(title);




	}
	public Map<Integer, List<Title>> getTitle() {
		return title;
	}




	public void setTitle(Map<Integer, List<Title>> title) {
		this.title = title;
	}




		// 用户解析
   	 	 private void uerstart()   {
   	 		String str=null;
   	 		BufferedReader rf= null;
			try {
				rf = new BufferedReader(
					new InputStreamReader(
							new FileInputStream("myuser.txt")));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
   	 		try {
				while((str=rf.readLine())!=null) {
					String n=str;
					String []nn=n.split(":");
					User uu=new User(nn[0],nn[1],nn[2],nn[3],nn[4]); 
					usermap.put(nn[0],uu);
 	 	
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					rf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	 	
   	 	}
   	 	public User getUserById(String id) {
   	 		return  usermap.get(id);
   	 				
   	 	}
   	 	
	
	// 考试规则解析
   	 	void rulestart() {
   	 		RandomAccessFile ra=null;
			try {
				ra = new RandomAccessFile("myrule.txt","rw");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
   	 		byte [] bs=new byte[10000];  

   	 		try {
				ra.read(bs);
			} catch (IOException e) {

				e.printStackTrace();
			}
//   	 		System.out.println(Arrays.toString(bs));
   	 		Rulee =new String(bs);

   	 	}
   	 	public String getRulee() {
			return this.Rulee;
   	 		
   	 	}
   	 	
   	 	
	
	// 其他文件内容的解析
	

	
	
}
